﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace VLUSM_DataAccess.Repositories
{
    public class DonViRepository : GenericRepository<MainDataContainer>
    {
        public DonViRepository(string connectionString) : base(connectionString, "MainData") { }
        public DonViRepository(MainDataContainer context) : base(context) { }

        public DonVi GetSingleByID(int IDFilter)
        {
            var query = this.GetFirst<DonVi>(d => d.ID == IDFilter);
            return query;
        }

        public DonVi GetSingleByTen(string TenFilter)
        {
            var query = this.GetFirst<DonVi>(d => d.TenGoi == TenFilter);
            return query;
        }

        public DonVi GetSingleByMoTa(string MoTaFilter)
        {
            var query = this.GetFirst<DonVi>(d => d.MoTa == MoTaFilter);
            return query;
        }

        public DonVi GetSingleByIsCapTruong(bool IsCapTruongFilter)
        {
            var query = this.GetFirst<DonVi>(d => d.IsCapTruong == IsCapTruongFilter);
            return query;
        }

        public IEnumerable<DonVi> GetAll()
        {
            var query = this.GetAll<DonVi>();
            return query;
        }

        public IEnumerable<DonVi> GetAllByCapTruong()
        {
            var query = this.GetAll<DonVi>().Where(d => d.IsCapTruong);
            return query;
        }

        public IEnumerable<DonVi> GetAllByNotCapTruong()
        {
            var query = this.GetAll<DonVi>().Where(d => !d.IsCapTruong);
            return query;
        }

        public IEnumerable<DonVi> GetAllByLoaiHoSoID(int LoaiHoSoIDFilter)
        {
            var query = this.Find<DonVi>(h => this.Any<LoaiHoSo>(d => d.ID == LoaiHoSoIDFilter));
            return query;
        }

        public void Save(DonVi obj)
        {
            try
            {
                if (obj.ID != 0)
                {
                    this.Attach<DonVi>(obj);
                }
                else
                {
                    this.Add<DonVi>(obj);
                }
            }
            catch { }
        }

        public void Remove(DonVi obj)
        {
            this.Delete<DonVi>(obj);
        }

        public void Remove(int IDFilter)
        {
            var obj = this.GetSingleOrDefault<CauLacBo>(c => c.ID == IDFilter);
            this.Delete<CauLacBo>(obj);
        }
    }
}
